# --- code : utf-8 --- #
# 该文件用.pyw作后缀命名，可阻止 DOS 命令行或终端窗口(cmd)弹出

# 功能：输入倒计时时间（输入参数：①小时，②分钟,格式都为 int），倒计时到后打开相应文件提醒
# 输入参数：inhour - 倒计时小时，inminute - 倒计时分钟
def simple_custom_reminder(inhour, inminute):
    Path = 'E:\\Python_file\\timeTask'
    timeOut = datetime.datetime(year=1949, month=10, day=1, hour=inhour, minute=inminute, second=0).strftime(
        '%H%M%S')  # 设置 timeout 时间并转换为字符串格式
    curTime = datetime.datetime.now().strftime('%H%M%S')  # 获取当前时间并转换为字符串格式
    remain = int(timeOut) - int(curTime)  # 计算时间差
    logging.info('The timeout is ' + str(timeOut))
    logging.info('The current time is ' + str(curTime))

    while 1:
        if remain < 0: # 计时小于0
            raise Exception('The remain is smaller than zero . ')
        # if remain > 30000: # 计时超过三小时
        #     raise Exception('The remain is longer than three hours . ')
        if remain == 0:
            time.sleep(1)
            os.chdir(Path)
            logging.info('Current working path is : ' + str(os.getcwd()))
            subprocess.Popen(['start', 'timeout.txt'], shell=True)  # 打开提醒文件
            break
        else:
            time.sleep(1)
            curTime = datetime.datetime.now().strftime('%H%M%S')  # 刷新系统当前时间
            remain = int(timeOut) - int(curTime)  # 刷新时间差
            print(time.ctime())

def timer1():
    logging.info('timer1 have been chosen .')
    hour = 9
    minute = 30
    int(hour)
    int(minute)
    logging.info('deadline :' + str(hour) + ':' + str(minute))
    simple_custom_reminder(hour, minute)

def timer2():
    logging.info('timer2 have been chosen .')
    hour = 14
    minute = 50
    int(hour)
    int(minute)
    logging.info('deadline :' + str(hour) + ':' + str(minute))
    simple_custom_reminder(hour, minute)

def timer3():
    hour = 17
    minute = 50
    int(hour)
    int(minute)
    simple_custom_reminder(hour, minute)

# 功能：创建提醒文件
def createTheReminder():
    # Path = 'E:\\Python_file\\timeTask'
    file = 'E:\\Python_file\\timeTask\\timeout.txt'

    logging.info('Is the file path exists ? :' + str(os.path.exists(file))) # 检查路径有效性
    # 创建/打开文件，写入内容
    openFile = open(file,'w')
    logging.info('creating the reminder file.' )
    openFile.write('1')
    openFile.close()
    logging.info('reminder file creation finish.')

def GUI():
    # GUI
    GUI_size = '250x130'
    GUI_title = 'Reminder'
    logging.info('creating GUI.')
    top = tkinter.Tk()
    top.geometry(GUI_size)
    top.title(GUI_title)
    logging.info('GUI size : ' +  GUI_size)
    logging.info('GUI title : ' + GUI_title)

    # creat Button
    logging.info('creating Button.')
    button_array = ['①0930', '②1450', '③1750', 'Quit']
    for button_title in button_array:
        logging.info('Button title :' + button_title)
    logging.info('setting Button command.')
    option1 = tkinter.Button(top, text=button_array[0], command=timer1, bg='LightGreen', activebackground='yellow')
    option2 = tkinter.Button(top, text=button_array[1], command=timer2, bg='Coral1', activebackground='blue')
    option3 = tkinter.Button(top, text=button_array[2], command=timer3, bg='LightBlue1', activebackground='green')
    option1.pack(fill=tkinter.X, expand=1)
    option2.pack(fill=tkinter.X, expand=1)
    option3.pack(fill=tkinter.X, expand=1)

    quit = tkinter.Button(top, text=button_array[3], command=top.quit, activebackground='red')
    quit.pack(fill=tkinter.X, expand=1)
    logging.info('GUI creation finish.')
    tkinter.mainloop()


def main():
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
    # logging.disable()  # 日志功能失效
    logging.info('program Laugh #_#')
    logging.info('Current working path is : ' + str(os.getcwd()))
    createTheReminder()
    GUI()


if __name__ == '__main__':
    import logging, os, time, subprocess, datetime, tkinter
    main()

# TODO ：优化重构代码，引入类
# TODO ：修复倒计时期间GUI卡死现象
# TODO ：增加点击GUI按钮后显示cmd倒计时可视化
# TODO ：增加 GUI 弹窗信息（时间差计算小于0）
# TODO : 添加提醒事项的输入选项（自定义提醒内容）
# TODO ：导出 log 文件

